package aModelLayer;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLDecoder;

public class Save {
	Game mModel;
	
	public Save(Game model) throws IOException {
		Inventory inventory = new Inventory();
		String path = Save.class.getProtectionDomain().getCodeSource().getLocation().getPath();
		StringBuilder decodedPath = new StringBuilder(URLDecoder.decode(path, "UTF-8"));
		decodedPath.replace(decodedPath.length() - 4, decodedPath.length() - 1, "src/aModelLayer/saveFile.properties");
		File file = new File(decodedPath.toString());
		DataOutputStream out = new DataOutputStream(new FileOutputStream(file,false));

		String line = ""; 
		if(inventory.getSize() > 0)
			for(int i = 0; i < inventory.getSize(); i++) {
				line = "Inventory." + i + "=" + inventory.get(i).getRef() + "\n";
				out.write(line.getBytes());
			}
		
		mModel = model;
		BattleCharacter[] battleCharacters = model.getBattleCharacters();
		
		for(int i = 0; i < 3; i++) {
			line = "BattleCharacter." + i + "0=" + battleCharacters[i].getCurrentHealth() + "\n";
			out.write(line.getBytes());
			line = "BattleCharacter." + i + "1=" + battleCharacters[i].getCurrentMana() + "\n";
			out.write(line.getBytes());
			line = "BattleCharacter." + i + "2=" + battleCharacters[i].getBaseMaxHealth() + "\n";
			out.write(line.getBytes());
			line = "BattleCharacter." + i + "3=" + battleCharacters[i].getBaseMaxMana() + "\n";
			out.write(line.getBytes());
			line = "BattleCharacter." + i + "4=" + battleCharacters[i].getBaseStrength() + "\n";
			out.write(line.getBytes());
			line = "BattleCharacter." + i + "5=" + battleCharacters[i].getBaseDefense() + "\n";
			out.write(line.getBytes());
			line = "BattleCharacter." + i + "6=" + battleCharacters[i].getBaseSpeed() + "\n";
			out.write(line.getBytes());
			line = "BattleCharacter." + i + "7=" + battleCharacters[i].getBaseMagic() + "\n";
			out.write(line.getBytes());
			line = "BattleCharacter." + i + "8=" + battleCharacters[i].getHeadEquip().getRef() + "\n";
			out.write(line.getBytes());
			line = "BattleCharacter." + i + "9=" + battleCharacters[i].getChestEquip().getRef() + "\n";
			out.write(line.getBytes());
			line = "BattleCharacter." + i + "10=" + battleCharacters[i].getWeaponEquip().getRef() + "\n";
			out.write(line.getBytes());
			line = "BattleCharacter." + i + "11=" + battleCharacters[i].getXP() + "\n";
			out.write(line.getBytes());
			line = "BattleCharacter." + i + "12=" + battleCharacters[i].getLevel() + "\n";
			out.write(line.getBytes());
		}
		
		System.out.println("lolsaved");
		
		out.close();
	}
}
